package com.example.qxfw.backgroundManagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.qxfw.backgroundManagement.mapper.MeteorologicalPersonnelInformationManagementMapper;
import com.example.qxfw.backgroundManagement.service.MeteorologicalPersonnelInformationManagementService;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.IssuerInfo;
import com.example.qxfw.common.entity.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/7/3  星期一   16:22
 * @description : 此类用来 气象人员信息管理
 ***************************************/
@Slf4j
@Api(tags = "后台管理-气象人员信息管理")
@RestController
@CrossOrigin
@RequestMapping("/meteorologicalPersonnelInformationManagement")
public class MeteorologicalPersonnelInformationManagementController {

    @Autowired
    private MeteorologicalPersonnelInformationManagementMapper meteorologicalPersonnelInformationManagementMapper;


    @Autowired
    private MeteorologicalPersonnelInformationManagementService meteorologicalPersonnelInformationManagementService;
    @ApiOperation("初始化人员信息列表")
    @GetMapping(value = "selectAllIssuerInfo")
    public SysResult selectAllIssuerInfo() {
        List<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementService.selectAllIssuerInfo();
        return SysResult.success(issuerInfos);
    }

    @ApiOperation("查询出签发人员信息表的所有权限")
    @GetMapping(value = "selectPermission")
    public SysResult selectPermission() {
        List<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementService.selectPermission();
        return SysResult.success(issuerInfos);
    }

    @ApiOperation("根据权限permission查询出签发人员信息")
    @GetMapping(value = "selectByPermission")
    public SysResult selectByPermission(int permission,int pageNo,int pageSize) {
        if (permission == 0) {
            IPage<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementService.selectByPermission(null,pageNo,pageSize);
            return SysResult.success(issuerInfos);
        }
        log.debug(String.valueOf(permission));
        IPage<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementService.selectByPermission(String.valueOf(permission),pageNo,pageSize);
        return SysResult.success(issuerInfos);
    }

    @ApiOperation("添加签发人员信息-传字段")
    @PostMapping(value = "insertIssuerInfo")
    public SysResult insertIssuerInfo(final String userName, final String phone, final String permission) {
        System.out.println(userName);
        System.out.println(phone);
        System.out.println(permission);
        List<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementService.selectByUserName(userName);
        if (issuerInfos != null && issuerInfos.size() > 0) {
            return SysResult.fail("该用户已存在");
        } else {
            Integer insertIssuerInfo = this.meteorologicalPersonnelInformationManagementService.insertIssuerInfo(userName, phone, permission);
            return returnInformation("添加签发人员信息", insertIssuerInfo);
        }
    }

    @ApiOperation("添加签发人员信息")
    @PostMapping(value = "addIssuerInfo")
    public SysResult addIssuerInfo(@RequestBody UserInfo UserInfo){
        List<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementService.selectByUserName(UserInfo.getUserName());
        if (issuerInfos != null && issuerInfos.size() > 0) {
            return SysResult.fail("该用户已存在");
        } else {
            Integer insertIssuerInfo = this.meteorologicalPersonnelInformationManagementService.insertIssuerInfo(UserInfo.getUserName(), UserInfo.getPhone(), UserInfo.getPermission());
            return returnInformation("添加签发人员信息", insertIssuerInfo);
        }
    }



    @ApiOperation("添加签发人员信息-传对象")
    @PostMapping(value = "insertIssuerInfo1")
    public SysResult insertIssuerInfo(@RequestBody  IssuerInfo issuerInfo) {
        List<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementService.selectByUserName(issuerInfo.getUsername());
        if (issuerInfos != null && issuerInfos.size() > 0) {
            return SysResult.fail("该用户已存在");
        } else {
            Integer insertIssuerInfo = this.meteorologicalPersonnelInformationManagementService.insertByAll(issuerInfo);
            return returnInformation("添加签发人员信息", insertIssuerInfo);
        }
    }
    @ApiOperation("更新签发人员信息")
    @PostMapping(value = "updateIssuerInfo")
    public SysResult updateIssuerInfo(final int id, final String userName, final String password, final String phone, final String permission) {
        List<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementMapper.selectById(id);
        if (issuerInfos != null && issuerInfos.size() > 0) {
            Integer updateIssuerInfo = this.meteorologicalPersonnelInformationManagementService.updateIssuerInfo(id, userName, password, phone, permission);
            return returnInformation("更新签发人员信息", updateIssuerInfo);
        } else {
            return SysResult.fail("无此签发人员");
        }
    }


    @ApiOperation("删除签发人员")
    @GetMapping(value = "deleteIssuerInfo")
    public SysResult deleteIssuerInfo(int id) {
        List<IssuerInfo> issuerInfos = this.meteorologicalPersonnelInformationManagementMapper.selectById(id);
        if (issuerInfos != null && issuerInfos.size() > 0) {
            Integer deleteIssuerInfo = this.meteorologicalPersonnelInformationManagementService.deleteIssuerInfo(id);
            return returnInformation("更新签发人员信息", deleteIssuerInfo);
        } else {
            return SysResult.fail("无此签发人员");
        }
    }


    public SysResult returnInformation(String info, Integer result) {
        if (result < 0) {
            return SysResult.fail(info + "失败");
        } else {
            return SysResult.success(result);
        }
    }

}
